﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace de.rwth.rz.campus.labs.Mailer2010.SPHelper
{
    /// <summary>
    /// This class contains some helper methods for string handling
    /// </summary>
    public class SPStringHelper
    {
        /// <summary>
        /// Splits a SPItemEventDataCollection object into a list of strings
        /// </summary>
        /// <param name="afterProperties">The SPItemEventDataCollection object</param>
        /// <returns>A list of strings which contains the values of the SPItemEventDataCollection object</returns>
        /// <example>List<String> toValues = ItemEventReceiverHelper.GetValues(properties.AfterProperties["Value"]);</example>
        public static List<String> GetValuesFrom(Object afterProperties)
        {
            List<String> items = new List<String>();
            if (afterProperties != null)
            {
                if (!String.IsNullOrEmpty(afterProperties.ToString()))
                {
                    try
                    {
                        SPFieldMultiColumnValue helper = new SPFieldMultiColumnValue(afterProperties.ToString());
                        for (int i = 0; i < helper.Count; i++)
                        {
                            items.Add(helper[i]);
                        }
                    }
                    catch (ArgumentException)
                    {
                        //just ignore undocumented argument exception
                    }
                }
            }
            return items;
        }

        /// <summary>
        /// Splits a semicola separated textfield.
        /// </summary>
        /// <param name="afterProperties">The semicola separated entries</param>
        /// <returns>A list of entries</returns>
        public static List<String> GetSemicolaSeparatedEntries(Object afterProperties)
        {
            List<String> others = new List<String>();
            if (afterProperties != null)
            {
                if (!String.IsNullOrEmpty(afterProperties.ToString()))
                {
                    String trimmedString = afterProperties.ToString().Replace(" ", String.Empty);
                    String[] items = trimmedString.Split(';');
                    foreach (String item in items)
                    {
                        if (!String.IsNullOrEmpty(item)) others.Add(item);
                    }
                }
            }
            return others;
        }

    }
}
